package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.commom.exception.CustomException;
import com.orientation.dto.BatchBedDto;
import com.orientation.dto.BedChangeDto;
import com.orientation.entity.Bed;
import com.orientation.entity.Room;
import com.orientation.service.BedService;
import com.orientation.service.RoomService;
import com.orientation.vo.BedMoveVo;
import com.orientation.vo.BedStudentVo;
import com.orientation.vo.StudentProgressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 报到 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@Api(tags = "床位信息")
@RestController
@RequestMapping("/bed")
public class BedController {
    @Resource
    private BedService bedService;
    @Resource
    private RoomService roomService;


    @ApiOperation("添加")
    @PostMapping
    public ResponseEntity<Result> insertRoom(@RequestBody Bed bed) {
        bedService.insertBed(bed);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("微调床位")
    @PutMapping
    public boolean updateTrimBed(@RequestBody Bed bed) {
        if (StringUtils.hasLength(bed.getStudentId())) {
            Bed bedOne = bedService.getOne(Wrappers.<Bed>lambdaQuery().eq(Bed::getStudentId, bed.getStudentId()));
            bedOne.setStudentId(null);
            bedService.updateById(bedOne);
        }
        return bedService.updateById(bed);
    }
    @ApiOperation("修改")
    @PutMapping("/update")
    public boolean update(@RequestBody Bed bed) {
        return bedService.updateById(bed);
    }
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        Bed bed = bedService.getById(id);
        Room room = roomService.getById(bed.getRoomId());
        room.setBed(room.getBed() == 1 ? 0 : room.getBed() - 1);
        roomService.updateById(room);
        return bedService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Bed findById(@PathVariable String id) {
        return bedService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Bed> findAll() {
        return bedService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<Bed> findPage(@RequestParam(required = false, defaultValue = "") String name,
                               @RequestParam(required = true, defaultValue = "") String roomId,
                               @RequestParam(required = false, defaultValue = "") String studentId,
                               @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "100") Integer pageSize) {
        if (!StringUtils.hasLength(roomId)) {
            throw new CustomException("18", "选择房间信息");
        }
        Room room = roomService.getById(roomId);
        IPage<Bed> bedIPage = bedService.page(new Page<>(pageNum, pageSize),
                Wrappers.<Bed>lambdaQuery().like(StringUtils.hasLength(name), Bed::getName, name)
                        .eq(StringUtils.hasLength(roomId), Bed::getRoomId, roomId)
                        .eq(StringUtils.hasLength(studentId), Bed::getStudentId, studentId));
        if (!CollectionUtils.isEmpty(bedIPage.getRecords())) {
            for (Bed bed : bedIPage.getRecords()) {
                bed.setRoomName(room.getName());
            }
        }
        return bedIPage;
    }

    @ApiOperation("床位分配进度")
    @GetMapping("/progressBed")
    public ResponseEntity<Result> progressBed(@RequestParam(required = false, defaultValue = "") String collegeId) {
        StudentProgressVo progress = bedService.progressBed(collegeId);
        return ResponseEntity.ok(Result.success(progress));
    }


    @ApiOperation("批量添加")
    @PostMapping("/insertBedBatch")
    public ResponseEntity<Result> insertBedBatch(@RequestBody List<Bed> bedList) {
        bedService.insertBedBatch(bedList);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("批量添加")
    @PostMapping("/insertBedBatchFile")
    public ResponseEntity<Result> insertBedBatchFile(
            @RequestParam(value = "files", required = false) MultipartFile[] files,
            @RequestParam(value = "names") List<String> names,
            @RequestParam(value = "roomId") String roomId) {
        bedService.insertBedBatchFile(files, names, roomId);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("床位信息")
    @GetMapping("/queryBedId")
    public ResponseEntity<Result> queryBed(@RequestParam(required = true, value = "bedId") String bedId) {
        BedStudentVo vo = bedService.queryBed(bedId);
        return ResponseEntity.ok(Result.success(vo));
    }

    @ApiOperation("床位信息列表")
    @GetMapping("/queryBedStudentList")
    public ResponseEntity<Result> queryBedStudentList(@RequestParam(required = true, value = "roomId") String roomId) {
        return ResponseEntity.ok(Result.success(bedService.queryBedStudentList(roomId)));
    }

    @ApiOperation("分配宿舍床位")
    @PostMapping("/distributionBed")
    public ResponseEntity<Result> distributionBed(@RequestBody BatchBedDto batchBedDto) {
        bedService.distributionBed(batchBedDto);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("按照房间查询")
    @GetMapping("/ByRoomId/{sid}")
    public List<Bed> queryBedList(@PathVariable String sid) {
        Room room = roomService.getById(sid);
        List<Bed> beds = bedService.list(Wrappers.<Bed>lambdaQuery().eq(Bed::getRoomId, sid));
        if (!CollectionUtils.isEmpty(beds)) {
            for (Bed bed : beds) {
                bed.setRoomurl(room.getRoomurl());
                break;
            }
        }
        return beds;
    }

    @ApiOperation("床位微调列表")
    @GetMapping("/queryBedMove")
    public ResponseEntity<Result> queryBedMove(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "majorId", required = false) String majorId,
                                               @RequestParam(value = "className", required = false) String className,
                                               @RequestParam(value = "studentName", required = false) String studentName,
                                               @RequestParam(value = "studentId", required = false) String studentId,
                                               @RequestParam(value = "sexType", required = true, defaultValue = "1") String sexType,
                                               @RequestParam(value = "adviserId", defaultValue = "") String adviserId) {
        IPage<BedMoveVo> bedMoveVoIPage = bedService.queryBedMove(pageNum, pageSize, majorId, className, studentName, studentId, sexType, adviserId);
        return ResponseEntity.ok(Result.success(bedMoveVoIPage));
    }

    @ApiOperation("按照类型查询楼栋床位")
    @GetMapping("/getBedList")
    public ResponseEntity<Result> getBuildingByType(String roomId) {
        List<Bed> bedList = bedService.list(Wrappers.<Bed>lambdaQuery().eq(Bed::getRoomId, roomId).isNull(Bed::getStudentId));
        return ResponseEntity.ok(Result.success(bedList));
    }

    @ApiOperation("宿舍床位交换")
    @PostMapping("/distributionChangeBed")
    public ResponseEntity<Result> distributionChangeBed(@RequestBody BedChangeDto bedChangeDto) {
        bedService.distributionChangeBed(bedChangeDto);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("移除床位")
    @GetMapping("/removeBedStudent")
    public ResponseEntity<Result> removeBedStudent(String studentId) {
        Bed bed = bedService.getOne(Wrappers.<Bed>lambdaQuery().eq(Bed::getStudentId, studentId));
        bed.setStudentId(null);
        bedService.updateById(bed);
        return ResponseEntity.ok(new Result());
    }
}

